package com.tyk.bookstore.back.message.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tyk.bookstore.back.common.core.model.page.PageVo;
import com.tyk.bookstore.back.common.core.model.result.R;
import com.tyk.bookstore.back.message.model.query.InfoNoticeAddQuery;
import com.tyk.bookstore.back.message.model.query.InfoNoticePageQuery;
import com.tyk.bookstore.back.message.model.query.InfoNoticeUpdateQuery;
import com.tyk.bookstore.back.message.model.vo.InfoNoticePageVo;
import com.tyk.bookstore.back.message.model.vo.InfoNoticeVo;
import com.tyk.bookstore.back.message.service.InfoNoticeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author tyk
 * @description 消息通知管理
 */
@RequestMapping({"api/message/notice/info"})
@Tag(name = "消息管理/信息公告", description = "信息公告管理")
@RestController
@RequiredArgsConstructor
public class InfoNoticeController {

    private final InfoNoticeService infoNoticeService;

    @Operation(summary = "查询信息公告详情", description = "获取信息公告详情")
    @SaCheckPermission("message:notice:info:get")
    @GetMapping({"{id}"})
    public R<InfoNoticeVo> getById(
            @Schema(description = "公告ID") @NotNull @Min(1)
            @PathVariable Long id
    ) {
        return R.ok(infoNoticeService.getById(id));
    }

    @Operation(summary = "分页查询信息公告", description = "分页查询信息公告")
    @SaCheckPermission("message:notice:info:page")
    @GetMapping({"page"})
    public R<InfoNoticePageVo> getPage(
            @Validated @ParameterObject InfoNoticePageQuery query, BindingResult br
    ) {
        return br.hasErrors() ? R.ok(PageVo.instance(InfoNoticePageVo.class)) : R.ok(infoNoticeService.getPage(query));
    }

    @Operation(summary = "添加信息公告", description = "添加信息公告")
    @SaCheckPermission("message:notice:info:add")
    @PostMapping
    public R<Object> add(
            @Validated @RequestBody InfoNoticeAddQuery query
    ) {
        infoNoticeService.add(query);
        return R.ok();
    }

    @Operation(summary = "更新信息公告", description = "更新信息公告")
    @SaCheckPermission("message:notice:info:update")
    @PutMapping
    public R<Object> updateById(
            @Validated @RequestBody InfoNoticeUpdateQuery query
    ) {
        infoNoticeService.updateById(query);
        return R.ok();
    }

    @Operation(summary = "删除信息公告", description = "删除信息公告")
    @SaCheckPermission("message:notice:info:delete")
    @DeleteMapping("{id}")
    public R<Object> deleteById(
            @Validated @PathVariable Long id
    ) {
        infoNoticeService.deleteById(id);
        return R.ok();
    }


}
